package com.tuozixuan.onesql.service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.tuozixuan.onesql.mysql.BaseDao;

public class TableServiceImpl implements TableService {
    
    public List<Map<String,Object>> getTableData(String tableName, String whereConditon, Long startNo, Long pageSize) {
        String queryStr = "select * from " + tableName;
        if (Objects.nonNull(whereConditon)) {
            queryStr += whereConditon;
        }
        if (Objects.nonNull(startNo) && Objects.nonNull(startNo) ) {
            queryStr += " limit " + startNo + ", " + pageSize;
        }
        
        List<Map<String,Object>> dataList = new BaseDao().query(queryStr);
        return dataList;
    }

    public long getTableDataCount(String tableName, String whereConditon) {
        String queryStr = "select count(*) from " + tableName;
        if (Objects.nonNull(whereConditon)) {
            queryStr += whereConditon;
        }
        
        long resultSize = new BaseDao().queryCount(queryStr);
        return resultSize;
    }

    @Override
    public List<Map<String, Object>> getDataBySql(String sql) {
        return new BaseDao().query(sql);
    }
    
}